package Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ClientHandler implements Runnable {
    private Socket clientSocket;
    private Connection connection;

    public ClientHandler(Socket clientSocket, Connection connection) {
        this.clientSocket = clientSocket;
        this.connection = connection;
    }

    @Override
    public void run() {
        try {
            // 创建输入输出流
            PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            // 读取客户端请求
            String request = in.readLine();
            if (request.equals("登录")) {
                String username = in.readLine();
                String password = in.readLine();
                if (authenticateUser(username, password)) {
                    out.println("登录成功");
                } else {
                    out.println("登录失败");
                }
            } else if (request.equals("注册")) {
                String username = in.readLine();
                String password = in.readLine();
                if (registerUser(username, password)) {
                    out.println("注册成功");
                } else {
                    out.println("注册失败");
                }
            } else if (request.equals("创建课程")) {
                String username = in.readLine();
                if (createCourse(username)) {
                    out.println("创建成功");
                } else {
                    out.println("创建失败");
                }
            }else if(request.equals("写作业")){
                sendMsgToAll();
            }

            // 关闭流和套接字
            in.close();
            out.close();
            clientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sendMsgToAll() {

    }

    private boolean authenticateUser(String username, String password) {
        try {
            Statement statement = connection.createStatement();
            String query = "SELECT * FROM admin WHERE username = '" + username + "' AND password = '" + password + "'";
            ResultSet resultSet = statement.executeQuery(query);
            boolean success = resultSet.next();
            resultSet.close();
            statement.close();
            return success;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    private boolean registerUser(String username, String password) {
        try {
            Statement statement = connection.createStatement();
            String query = "insert into admin (username, password) values ('" + username + "', '" + password + "')";
            int rowsAffected = statement.executeUpdate(query);
            statement.close();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    private boolean createCourse(String usename){

        return false;
    }
}